package com.yonyou.gmmc.service.questionnaire.controller;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.marketing.common.constants.CommonConstant;
import org.marketing.common.constants.ResultMsgConstants;
import org.marketing.common.exception.ServiceBizException;
import org.marketing.common.util.CheckUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import org.marketing.common.Response;
import com.yonyou.gmmc.service.questionnaire.constant.QuestionnaireConstant;
import com.yonyou.gmmc.service.questionnaire.dto.NaireCommit;
import com.yonyou.gmmc.service.questionnaire.dto.QustionShare;
import com.yonyou.gmmc.service.questionnaire.service.QuestionnaireService;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

/**
 *
 * @author Administrator 
 * @date 2017年3月7日
 */

@RestController
@RequestMapping(value = QuestionnaireConstant.BASE_PATH + "/" + QuestionnaireConstant.VERSION)
@Api(value = "问卷调查")
public class QuestionnaireController {
	private Logger logger = LoggerFactory.getLogger(this.getClass());
	@Autowired
	private QuestionnaireService questionnaireService;

	
	@RequestMapping(value = "/questionType", method = RequestMethod.GET)
	@ApiOperation(value = "问卷类型", notes = "查询问卷类型")
	@ResponseBody
	public Response<List<Map<String, Object>>> questionType(
			@RequestParam(value = "queryType", required = true) Integer queryType,
            @RequestParam(value = "userId_", required = false) String userId_,
			HttpServletRequest request) {
		logger.info("--问卷类型--");
		String userId="";
		if(!CheckUtil.NullOrEmpty(userId_)){
			userId = userId_;
    	}else{
    		userId = request.getHeader(CommonConstant.HEAD_USER_ID);
    	}
		String openId = request.getHeader(CommonConstant.HEADER_OPENID);
		List<Map<String, Object>> list = questionnaireService.getQnaireType(queryType,userId,openId);
		return new Response<List<Map<String, Object>>>(QuestionnaireConstant.RESULT_CODE, list, null);
	}

	
	@RequestMapping(value = "/explainView", method = RequestMethod.GET)
	@ApiOperation(value = "介绍页面", notes = "查询介绍页面信息")
	@ResponseBody
	public Response<Map<String, Object>> explainView(@RequestParam(value = "qneId", required = true) Integer qneId) {
		logger.info("--介绍页面--");
		Map<String, Object> list = questionnaireService.getExplainVies(qneId);
		return new Response<Map<String, Object>>(QuestionnaireConstant.RESULT_CODE, list, null);
	}

	@SuppressWarnings("unchecked")
	
	@RequestMapping(value = "/questionNaireDetail", method = RequestMethod.GET)
	@ApiOperation(value = "问卷详情", notes = "查询问卷详情信息")
	@ResponseBody
	public Response<Map<String, Object>> questionNaireDetail(
			@RequestParam(value = "qneId", required = true) Integer qneId,
			@RequestParam(value = "userId_", required = false) String userId_,
			HttpServletRequest request) {
		logger.info("--问卷详情--");
		String userId="";
		if(!CheckUtil.NullOrEmpty(userId_)){
			userId = userId_;
    	}else{
    		userId = request.getHeader(CommonConstant.HEAD_USER_ID);
    	}
		String openId = request.getHeader(CommonConstant.HEADER_OPENID);
		Map<String, Object> map = questionnaireService.questionNaireDetail(qneId, userId,openId);
		Map<String, Object> rsMap = new HashMap<>();
		// 构造前台所须数据格式
		List<Map<String, Object>> pages = (List<Map<String, Object>>) map.get("pages");
		rsMap.put("pages", pages);
		// 构造格式结束
		return new Response<Map<String, Object>>(QuestionnaireConstant.RESULT_CODE, rsMap, null);
	}

	@SuppressWarnings("unchecked")
	
	@RequestMapping(value = "/questionResultDetail", method = RequestMethod.GET)
	@ApiOperation(value = "已答问卷详情", notes = "查询已答问卷详情信息")
	@ResponseBody
	public Response<Map<String, Object>> questionResultDetail(
			@RequestParam(value = "qneId", required = true) Integer qneId,
			@RequestParam(value = "userId_", required = false) String userId_,
			HttpServletRequest request) {
		logger.info("--问卷详情--");
		String userId="";
		if(!CheckUtil.NullOrEmpty(userId_)){
			userId = userId_;
    	}else{
    		userId = request.getHeader(CommonConstant.HEAD_USER_ID);
    	}
		String openId = request.getHeader(CommonConstant.HEADER_OPENID);
		Map<String, Object> map = questionnaireService.questionNaireDetail(qneId, userId,openId);
		Map<String, Object> rtMap = new HashMap<>();
		Map<String, Object> rsMap = new HashMap<>();
		// 构造前台所须数据格式
		Map<String, Object> asMap = (Map<String,Object>) map.get("asMap");
		List<Map<String, Object>> pages = (List<Map<String, Object>>) map.get("pages");
		rsMap.put("pages", pages);
		
		rtMap.put("pg", rsMap);
		rtMap.put("as", asMap);
		// 构造格式结束
		return new Response<Map<String, Object>>(QuestionnaireConstant.RESULT_CODE, rtMap, null);
	}
	
	
	@RequestMapping(value = "/commitNaire", method = RequestMethod.POST)
	@ApiOperation(value = "提交问卷", notes = "提交问卷信息")
	@ResponseBody
	public Response<Map<String, Object>> commitNaire(@RequestBody NaireCommit commit
			,@RequestParam(value = "userId_", required = false) String userId_
			,HttpServletRequest request) {
		logger.info("--提交问卷--");
		String userId="";
		if(!CheckUtil.NullOrEmpty(userId_)){
			userId = userId_;
    	}else{
    		userId = request.getHeader(CommonConstant.HEAD_USER_ID);
    	}
		String openId = request.getHeader(CommonConstant.HEADER_OPENID);
        NaireCommit naireCommit = new NaireCommit();
        naireCommit.setAnswer(commit.getAnswer());
        naireCommit.setQneId(commit.getQneId());
        naireCommit.setQuestionId(commit.getQuestionId());
        naireCommit.setSfdt(commit.getSfdt());
        naireCommit.setTwoId(commit.getTwoId());
        naireCommit.setUserId(commit.getUserId());
        //String openId="oXrcfuGP_a3daNZaMT_rbhvJ595I";
		questionnaireService.commitNaire(naireCommit,userId,openId);
		return new Response<Map<String, Object>>(QuestionnaireConstant.RESULT_CODE, null, null);
	}

	@RequestMapping(value = "/share", method = RequestMethod.POST)
	@ApiOperation(value = "分享内容保存", notes = "分享内容保存")
	public Response<Map<String, Object>> share(@RequestBody QustionShare qustionShare) {
		logger.info("--分享内容保存--");
		QustionShare qustionShare1 = new QustionShare();
		qustionShare1.setBizId(qustionShare.getBizId());
		qustionShare1.setBizName(qustionShare.getBizName());
		qustionShare1.setContent(qustionShare.getContent());
		qustionShare1.setShareTo(qustionShare.getShareTo());
		qustionShare1.setShareType(qustionShare.getShareType());
		qustionShare1.setSsid(qustionShare.getSsid());
		qustionShare1.setUserId(qustionShare.getUserId());
		questionnaireService.share(qustionShare1);
		return new Response<Map<String, Object>>(QuestionnaireConstant.RESULT_CODE, null, null);
	}

    /**
    * 问卷
    * @author Administrator
    * @date 2017年3月23日
    * @param request
    * @return
    * @throws Exception
    */
    
//    @RequestMapping(value = "/questionnaire",method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
//    public List<QuestionnaireaDto> queryQuestionnaire(HttpServletRequest request) {
//        String openId = request.getHeader(QuestionnaireConstant.HEADER_OPENID);
//        //String openId = "omLPIwJ06vhc8r2h2PRzL6wN7QNU";
//        List<QuestionnaireaDto> dtoList = questionnaireService.queryQuestionnaire(openId);
//        return dtoList;
//    }
    
    /**
    * 推送调查问卷
    * @author Administrator
    * @date 2017年6月2日
    * @return
    * @throws Exception
    */
//    @RequestMapping(value="/message/questionnaire",method = RequestMethod.GET, produces = "application/json;charset=UTF-8")
//    @ApiOperation(value = "推送调查问卷", notes = "推送调查问卷")
//    public Map<String,Object> sendQuestionnaire() {
//        Map<String,Object> result = new HashMap<>();
//        try{
//            result = questionnaireService.sendQuestionnaire();
//        }catch(Exception e){
//            logger.error(e.getMessage(), e);
//            throw new ServiceBizException(e.getMessage(), e);
//        }
//        return result;
//    }
    /**
	 * 绑定问卷和发送人
	 * 
	 * @author Administrator 
	 * @date 2017年4月17日
	 * @return
	 */
	@SuppressWarnings({ "rawtypes" })
	@RequestMapping(value = "/bindQuestionUser", method = RequestMethod.GET)
	@ApiOperation(value = "绑定问卷和发送人", notes = "绑定问卷和发送人")
	public Response bindQuestionUser(@RequestParam(required=true,defaultValue="") String qnt
			,@RequestParam(required=true,defaultValue="") List<String> list) {
		logger.info("QuestionnaireController.bindQuestionUser==>start...");
		try {
			Map<String,Object> map = new HashMap<>();
			map.put("qnt", qnt);
			map.put("list", list);
			questionnaireService.bindQuestionUser(map);
			return new Response(ResultMsgConstants.RESULT_CODE_SUCCESS, "");
		} catch (Exception e) {
			logger.error("查询状态发生异常", e);
			return new Response(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
		} finally {
			logger.info("QuestionnaireController.bindQuestionUser==>end...");
		}
	}
	
	/**
	 * 绑定问卷和发送人
	 * 
	 * @author Administrator 
	 * @date 2017年4月17日
	 * @return
	 */
	@SuppressWarnings({ "rawtypes" })
	@RequestMapping(value = "/bindNewUser", method = RequestMethod.GET)
	@ApiOperation(value = "绑定问卷和新注册用户", notes = "绑定问卷和新注册用户")
	public Response bindNewUser(){
		logger.info("QuestionnaireController.bindQuestionUser==>start...");
		try {
			questionnaireService.bindNewUser();
			return new Response(ResultMsgConstants.RESULT_CODE_SUCCESS, "");
		} catch (Exception e) {
			logger.error("查询状态发生异常", e);
			return new Response(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
		} finally {
			logger.info("QuestionnaireController.bindQuestionUser==>end...");
		}
	}
	/**
	 * 自动更新新注册用户的问卷绑定的ID
	 * 
	 * @author Administrator 
	 * @date 2017年4月17日
	 * @return
	 */
	@SuppressWarnings({ "rawtypes" })
	@RequestMapping(value = "/updatePotentialToUser", method = RequestMethod.GET)
	@ApiOperation(value = "自动更新新注册用户的问卷绑定的ID", notes = "自动更新新注册用户的问卷绑定的ID")
	public Response updatePotentialToUser(){
		logger.info("QuestionnaireController.updatePotentialToUser==>start...");
		try {
			questionnaireService.updatePotentialToUser();
			return new Response(ResultMsgConstants.RESULT_CODE_SUCCESS, "");
		} catch (Exception e) {
			logger.error("查询状态发生异常", e);
			return new Response(ResultMsgConstants.RESULT_CODE_ERROR, e.getMessage());
		} finally {
			logger.info("QuestionnaireController.updatePotentialToUser==>end...");
		}
	}
}
